package bb.lanxing.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.util.DensityUtil;
import bb.lanxing.util.Log;

public class ShareViewHorizontal extends LinearLayout {
    public static final int TYPE_QQ = 0;
    public static final int TYPE_QZONE = 1;
    public static final int TYPE_WEIXIN = 2;
    public static final int TYPE_WX_CIRCLE = 3;
    public static final int TYPE_WEIBO = 4;
    public static final int TYPE_CHAT = 5;
    public static final int TYPE_LOCAL = 6;
    public static final int TYPE_OTHER = 7;
    public static final int TYPE_WEB = 8;
    public static final int TYPE_COPY = 9;
    private ShareItemClickListener mListener;
    private ShareAdapter shareAdapter;
    public static final int[] DEFAULT_TYPES = {TYPE_QQ, TYPE_QZONE, TYPE_WEIXIN, TYPE_WX_CIRCLE,
            TYPE_WEIBO, TYPE_LOCAL, TYPE_OTHER};
    private static final int[] icons = {R.drawable.ic_share_qq, R.drawable.ic_share_qzone,
            R.drawable.ic_share_wechat, R.drawable.ic_share_wechat_circle,
            R.drawable.ic_share_sina, R.drawable.ic_share_local,
            R.drawable.ic_share_local, R.drawable.ic_menu_more_gray,
            R.drawable.ic_share_web, R.drawable.item_share_copy};
    private static final String[] textRes = App.getContext().getResources().getStringArray(R.array.share_items);


    public interface ShareItemClickListener extends ShareView.ShareItemClickListener {
        @Override
        void onItemClick(int type);
    }

    public void setShareItemClickListener(ShareItemClickListener listener) {
        this.mListener = listener;
    }

    public ShareViewHorizontal(Context context) {
        super(context);
        init(context);
    }

    public ShareViewHorizontal(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ShareViewHorizontal(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void buildType(int[] types) {
        ArrayList arrayList = new ArrayList(types.length);
        for (int i : types) {
            arrayList.add(new ShareType(i));
        }
        this.shareAdapter.update(arrayList, true);
    }

    public void init(Context context) {
        RecyclerView recyclerView = (RecyclerView) LayoutInflater.from(context).inflate(R.layout.share_view_horizontal_layout, (ViewGroup) null);
        addView(recyclerView, new ViewGroup.LayoutParams(-1, -2));
        this.shareAdapter = new ShareAdapter(context, new ArrayList(9));
        recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false));//0
        recyclerView.setAdapter(this.shareAdapter);
        buildType(DEFAULT_TYPES);
        this.shareAdapter.setOnItemClickListener(new ShareAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                ShareType item = ShareViewHorizontal.this.shareAdapter.getItem(position);
                if (ShareViewHorizontal.this.mListener != null) {
                    ShareViewHorizontal.this.mListener.onItemClick(item.type);
                }
            }
        });
    }

    public class ShareType {
        int iconRes;
        String text;
        int type;

        public ShareType(int type) {
            this.type = type;
            this.text = ShareViewHorizontal.textRes[type];
            this.iconRes = ShareViewHorizontal.icons[type];
        }
    }

    public static class ShareViewHolder extends RecyclerView.ViewHolder {
        private ImageView shareIconView;
        private TextView shareTextView;
        private LinearLayout shareViewGroup;

        private void bindView(View bindSource) {
            this.shareViewGroup = (LinearLayout) bindSource.findViewById(R.id.item_share_ll);
            this.shareIconView = (ImageView) bindSource.findViewById(R.id.item_share_icon);
            this.shareTextView = (TextView) bindSource.findViewById(R.id.item_share_text);
        }

        public ShareViewHolder(View itemView) {
            super(itemView);
            bindView(itemView);
        }
    }

    public static class ShareAdapter extends RecyclerView.Adapter<ShareViewHolder> {
        protected Context context;
        protected List<ShareType> dataSet;
        protected OnItemClickListener onItemClickListener;

        public interface OnItemClickListener {
            void onItemClick(int position);
        }

        public ShareAdapter(Context context, List<ShareType> dataSet) {
            new ArrayList();
            this.context = context;
            this.dataSet = dataSet;
        }

        @Override
        public ShareViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ShareViewHolder(LayoutInflater.from(this.context).inflate(R.layout.item_horizontal_share_view, parent, false));
        }

        @Override
        public void onBindViewHolder(ShareViewHolder holder, int position) {
            ShareType item = getItem(position);
            holder.shareIconView.setImageResource(item.iconRes);
            holder.shareTextView.setText(item.text);
            holder.shareViewGroup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ShareAdapter.this.onItemClickListener != null) {
                        ShareAdapter.this.onItemClickListener.onItemClick(position);//todo sst need find solution
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            List<ShareType> list = this.dataSet;
            if (list != null) {
                return list.size();
            }
            return 0;
        }

        public ShareType getItem(int position) {
            return this.dataSet.get(position);
        }

        public void update(List<ShareType> data, boolean clear) {
            if (clear) {
                this.dataSet.clear();
            }
            this.dataSet.addAll(data);
            notifyDataSetChanged();
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.onItemClickListener = onItemClickListener;
        }


    }

    static class SpaceItemDecoration extends RecyclerView.ItemDecoration {
        public static String TAG = "SpaceItemDecoration";
        int mSpace;

        public SpaceItemDecoration(int space) {
            this.mSpace = DensityUtil.dp2px(space);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int itemCount = ((LinearLayoutManager) parent.getLayoutManager()).getItemCount();
            int childAdapterPosition = parent.getChildAdapterPosition(view);
            String str = TAG;
            Log.d(str, "itemCount>>" + itemCount + ";Position>>" + childAdapterPosition);
            outRect.left = 0;
            outRect.top = 0;
            outRect.bottom = 0;
            if (childAdapterPosition != itemCount - 1) {
                outRect.right = this.mSpace;
            } else {
                outRect.right = 0;
            }
        }
    }
}
